package com.bluesky.plum.sampleForms;

import java.lang.reflect.Method;

import com.bluesky.plum.richdomain.RichDomainMethod;
import com.bluesky.plum.uimodels.standard.UIComponentBuilder;
import com.bluesky.plum.uimodels.standard.components.containers.form.Form;
import com.bluesky.plum.uimodels.standard.components.containers.form.FormBuilder;

/**
 * a form to let user input info of a person and save it
 * 
 * @author HZ00260
 * 
 */
public class CreatePersonForm implements FormBuilder {

	@Override
	public Form createForm(UIComponentBuilder uiComponentBuilder,
			RichDomainMethod rdm) {
		// TODO Auto-generated method stub
		return null;
	}
//	@Override
//	public Form create(Method method) {
//		// TODO Auto-generated method stub
//		return null;
//	}

//	private RichDomainFields rdo = null;
//	private FormBuilder formBuilder;
//
//	public CreatePersonForm(FormBuilder formBuilder, RichDomainFields rdo) {
//		this.formBuilder = formBuilder;
//		this.rdo = rdo;
//	}
//
//	@Override
//	public Form createForm() {
//
//		final Form form = formBuilder.createForm(rdo);
//
//		ListBox sexListBox = (ListBox) form.getChildComponentByName("sex");
//		sexListBox.SetOptions(Sex.values());
//
//		final ListBox cityListBox = (ListBox) form.getChildComponentByName("city");
//		cityListBox.SetOptions(City.allCities.toArray());
//		cityListBox.setSelectChangeListener(new EventListener() {
//			@Override
//			public void process() {
//				System.out.println("you changeed the city to :"
//						+ cityListBox.getSelectedValue().toString());
//
//			}
//		});
//
//		// 2 buttons
//		final Button okBtn = (Button) formBuilder.getUiComponentBuilder()
//				.createNonFieldComponent(StandardUIComponentType.Button);
//		okBtn.setCaption("OK");
//		okBtn.setName("ok");
//		form.addChildComponent(okBtn);
//
//		final Button cancelBtn = (Button) formBuilder.getUiComponentBuilder()
//				.createNonFieldComponent(StandardUIComponentType.Button);
//		cancelBtn.setCaption("Cancel");
//		cancelBtn.setName("cancel");
//		form.addChildComponent(cancelBtn);
//
//		// 3 object browser
//		final ObjectBrowser personBrowser = (ObjectBrowser) formBuilder
//				.getUiComponentBuilder().createNonFieldComponent(
//						StandardUIComponentType.ObjectBrowser);
//		UIComponent personBrowserComp = (UIComponent)personBrowser;
//		personBrowserComp.setName("personBrowser");
//		personBrowserComp.setCaption("Person Browser");
//		personBrowser.setObjectRepository(RepositoryFactory.getRepository(Person.class));
//				
//				
////		personBrowser.setVisibleFieldNames(FieldUtil
////				.getFieldNames(new Person()));
//		personBrowser.setSelectionChangeListener(new EventListener() {
//			@Override
//			public void process() {
//				System.out.println("object browser ... selection change.");
//				form.readPojo(personBrowser.getSingleSelection());
//			}
//		});
//		form.addChildComponent(personBrowserComp);
//
//		okBtn.setClickListener(new EventListener() {
//			@Override
//			public void process() {
//
//				// update states of the fields
//				TextBox name = (TextBox) form.getChildComponentByName("name");
//				CheckBox married = (CheckBox) form.getChildComponentByName("married");
//
//				ListBox sex = (ListBox) form.getChildComponentByName("sex");
//				ListBox city = (ListBox) form.getChildComponentByName("city");
//
//				System.out.println("button " + okBtn.getCaption()
//						+ " is clicked");
//				System.out.println("the values are ");
//
//				System.out.println("name: " + name.getText());
//				System.out.println("married: " + married.isChecked());
//				System.out.println("sex: "
//						+ ((Sex) sex.getSelectedValue()).toString());
//				System.out.println("city: "
//						+ ((City) city.getSelectedValue()).toString());
//
//				/*
//				 * above is not my OUM final goal. my final goal is like code
//				 * below. <code> update(UpdateDirection.Back);//means from
//				 * native UI to domain model
//				 * 
//				 * rdo.updatePOJO();//here update the related POJO: personJack
//				 * PersonService.save(personJack); </code>
//				 */
//				Person p2 = new Person();
//				// rdo.generatePojo(p2);
//				form.writePojo(p2);
//				System.out.println("returned Person: ");
//				System.out.println(p2);
//
//			}
//		});
//
//		cancelBtn.setClickListener(new EventListener() {
//			@Override
//			public void process() {
//				System.out.println("button " + cancelBtn.getCaption()
//						+ " is clicked");
//
//			}
//		});
//
//		// add event map, format: [component].[event]
//		// form.getEventMap().put("ok.click", okBtn.getClickListener());
//		// form.getEventMap().put("cancel.click", cancelBtn.getClickListener());
//
//		return form;
//	}
//
//	public static void main(String[] args) {
//		Person jack = Person.getSample();
//		RichDomainFields rdo = new RichDomainFields().setEntity(jack);
//
//		FormCreator formCreator;
//		Form form;
//
////		// HTML
////		formCreator = new CreatePersonForm(FormBuilderFactory
////				.getFormBuilder(FormBuilderType.HTML), rdo);
////
////		formCreator.createForm().print();
////
////		// Swing
////		formCreator = new CreatePersonForm(FormBuilderFactory
////				.getFormBuilder(FormBuilderType.Swing), rdo);
//
//		form = formCreator.createForm();
//		JFrame frame = (JFrame) form.getNativeComponent();
//		frame.setSize(800, 600);
//
//		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//		frame.setVisible(true);
//	}
}
